package com.sun.module_base.ext

import android.view.View
import androidx.compose.foundation.clickable
import androidx.compose.ui.Modifier

/**
 *@author:sunc
 *@time :2025/7/21 11:08
 *@desc: 描述
 */
/**
 * 防止重复点击,可同时注册多个view
 */
fun setNoRepeatClick(vararg views: View?, interval: Long = 400, onClick: (View) -> Unit) {
    views.forEach {
        it?.clickNoRepeat(interval = interval) { view ->
            onClick.invoke(view)
        }
    }
}

/**
 * 防止重复点击
 * @param interval 重复间隔
 * @param onClick  事件响应
 */
var lastTime = 0L
fun View.clickNoRepeat(interval: Long = 400, onClick: (View) -> Unit) {
    setOnClickListener {
        val currentTime = System.currentTimeMillis()
        if (lastTime != 0L && (currentTime - lastTime < interval)) {
            return@setOnClickListener
        }
        lastTime = currentTime
        onClick(it)
    }
}

/**
 * 防止重复点击
 * @param interval 重复间隔
 * @param onClick  事件响应
 */
fun Modifier.click(interval: Long = 400, onClick: () -> Unit): Modifier {
   return this then Modifier.clickable {
        val currentTime = System.currentTimeMillis()
        if (lastTime != 0L && (currentTime - lastTime < interval)) {
            return@clickable
        }
        lastTime = currentTime
        onClick.invoke()
    }
}
